<?php
global $VALUTA;
global $USER_SETTING;

$kro = !isset($params['kro']) || $params['kro'];
$actions = !isset($params['actions']) || $params['actions'];
$balance = !isset($params['balance']) || $params['balance'];
$platezh = isset($params['platezh']) && $params['platezh'];
$show_frozen = isset($params['show_frozen']) && $params['show_frozen'];
$link_user_info = isset($params['link_user_info']) && $params['link_user_info'];
$show_time_line = isset($params['show_time_line']) && $params['show_time_line'];
$oper_info = isset($params['oper_info']) && $params['oper_info'];

$CI = &get_instance();
$me = $CI->MUser->get(Auth::$user_id);
$root = $CI->MUser->getRoot();

$all_opers = array();
?>

<script language="javascript" src='<?=config_item('js_path')?>js/table_list.js?<?=time()?>'></script>
<script language="javascript">


$(document).ready(function(){
	<?php if($actions || $oper_info) { ?>
		$(".o_actions tbody tr").each(function(index, element){
			var oId = $(this).attr('unitId');
			var uId = $(this).attr('user_id');
			var o = Opers[oId];
			var html = '';
			html += '<a href="oper/info/' + oId + '" title="Полная инфорация" onclick=\"operInfo(' + oId + '); return false;\"><img src="<?=config_item('image_path')?>images/icons/application_list.png"></a>';
			<?php if($actions) { ?>
				if(o.e) html += ' &nbsp;<a href="oper/edit/' + oId + '" title="Редактирование"><img src="<?=config_item('image_path')?>images/icons/edit.gif"></a>';
				if(o.m) html += ' &nbsp;<a href="modify/index/' + uId + '/' + oId + '" title="Переоформление"><img src="<?=config_item('image_path')?>images/icons/arrow_return_180.png"></a>';
				if(o.s) html += ' &nbsp;<a href="sell/index/' + uId + '/' + oId + '" title="Продажа"><img src="<?=config_item('image_path')?>images/icons/money.gif"></a>';
				if(o.rd) html += ' &nbsp;<a href="oper/recount_des/' + oId + '" title="Пересчитать реферальные бонус и бонусы руководителям"><img src="<?=config_item('image_path')?>images/icons/arrow_turn_090_left.png"></a>';
				if(o.d) html += ' &nbsp;<a href="oper/delete/' + oId + '" title="Удалить"><img src="<?=config_item('image_path')?>images/icons/delete.gif"></a>';
				if(o.f) html += ' &nbsp;<a href="oper/recount_frozen/' + oId + '" title="Обновить срок и сумму разморозки"><img src="<?=config_item('image_path')?>images/icons/snow.png"></a>';
			<?php } ?>
			this.cells[this.cells.length - 1].innerHTML = html;
			$(this.cells[this.cells.length - 1]).addClass('o_actions');
		});
	<?php } ?>

});


</script>

<?php foreach($users as $user) { 
	if(@$user['operations'] && count($user['operations']))
	{
		$begin_link_user_info = $link_user_info ? "<a href='user/info/".$user['id']."'>"  : '';
		$end_link_user_info = $link_user_info ? "</a>"  : '';
//		$user_actions = $->MUser->isCanOper($user['id'], false);
		?><a name="u<?=$user['id']?>"></a><div class='users_delimiter'></div>
<div style="text-align:center">
<span class='title1' style="margin-left:30px;"><?=$begin_link_user_info.$user['fio'].$end_link_user_info?></span>
<?php if($kro) { 
	$kro_link = @$params['kro_link'] ? $params['kro_link'] : 'kro/user/'.$user['id'];
	?>
	<a href='<?=$kro_link?>' style="margin-left:10px;"><img src='<?=config_item('image_path')?>images/excel.gif' width='16' height='16' title='Таблица КРО'></a>
<?php } ?>
<?php if($actions && ($user['parent_id'] == Auth::$user_id || $user['id'] == $root['id'] || ($user['id'] == Auth::$user_id && $me['can_oper']))) { ?>
	<a href='buy/index/<?=$user['id']?>' style="margin-left:10px;"><img src='<?=config_item('image_path')?>images/icons/money_plus.png' width='16' height='16' title='Оформить покупку мавро'></a>
	<a href='modify/index/<?=$user['id']?>' style="margin-left:10px;"><img src='<?=config_item('image_path')?>images/icons/arrow_return_180.png' width='16' height='16' title='Оформить переоформление мавро'></a>
	<a href='sell/index/<?=$user['id']?>' style="margin-left:10px;"><img src='<?=config_item('image_path')?>images/icons/money.gif' width='16' height='16' title='Оформить продажу мавро'></a>
<?php } ?>
<br>
<?=$user['status']?>  <span style='margin-left:30px;'><a href='mailto:<?=$user['email']?>'><?=$user['email']?></a> </span><?php if($user['phone']) echo "<span style='margin-left:10px;'>(Тел: ".$user['phone'].")</span>" ?>
<?php if($user['referer_email']) { ?>
	<span style='margin-left:100px;'>Реферер: <span style='margin-right:10px;'><?=$user['referer_fio']?></span> <?=$user['referer_email']?></span>
<?php } ?>
</div>
<table class='list operations <?=($actions || $oper_info ? 'o_actions' : '')?>' id='table_<?=$user['id']?>' cellpadding=0 cellspacing=0 user_id="<?=$user['id']?>"><thead>
<tr><th>Дата</th><th>Сумма</th><th>Курс</th><th>Мавро</th><?php if($show_frozen) { ?><th>Срок разморозки</th><th>Сумма при разморозке</th><?php } ?><th>Вид и место хранения средств</th><th>Дополнительно</th><?php if($platezh) { ?><th>Платёж</th><?php } ?><?php if($actions || $oper_info) { ?><th></th><?php } ?></tr></thead><tbody><?
foreach($user['operations'] as $oper) 
{
	$type_class = isBuy($oper) ? 'buy' : 'sell';
	if(isBuy($oper) && $oper['ostatok'] <= 0) $type_class .= ' selled';
	$is_bonus = $oper['bonus'] && $oper['type'] != 'modify.buy' && $oper['type'] != 'modify.sell';
	$super = $oper['depo'] && substr($oper['percent'], -1) == 5 ? "super" : '';
	if($show_frozen)
	{
		$frozen_gray = $oper['frozen_date'] && $oper['frozen_date'] <= date('Y-m-d') ? 'gray' : '';
		$show_frozen_row = $oper['frozen_summ'] && $oper['frozen_date'] && $oper['ostatok'];
	}
	if($actions)
	{
		$all_opers[$oper['id']] = array(
			'm' => canModify($oper, $user) ? 1 : '',
			's' => canSell($oper, $user) ? 1 : '',
			'e' => 1,
			'd' => canDelete($oper, $user) ? 1 : '',
			'rd' => canRecountDes($oper, $user) ? 1 : '',
			'f' => canRecountFrozen($oper) ? 1 : ''
		);
	}
?><tr user_id="<?=$user['id']?>" unitId='<?=$oper['id']?>'><td class='<?=$is_bonus ? 'bonus' : ''?> o_date'><?=getPrintDate($oper['date'])?></td><td class='<?=$is_bonus ? 'bonus' : ''?> <?=$type_class?> o_summ'><?=number_format(round($oper['summ'], 2), 2, $USER_SETTING['znak_drob'], ' ')?> <?=$VALUTA[$oper['valuta']]?></td><td class='<?=$is_bonus ? 'bonus' : ''?> <?=$type_class?> o_kurs'><?php if($oper['kurs_valuta'] > 0 && $oper['kurs_valuta'] != 1) echo round($oper['kurs_valuta'], 4)." / "; ?><?php if($oper['type'] != 'week' && $oper['mavro'] > 0) { ?> <?=$oper['kurs']?> (<?=$oper['percent']?>%)<?php } else { ?><?=$oper['percent']?>%<?php } ?></td><td class='<?=$is_bonus ? 'bonus' : ''?> <?=$type_class?> o_mavro'><?php if($oper['type'] != 'week' && $oper['mavro'] > 0) { ?><?php if(isBuy($oper) && $oper['ostatok'] && $oper['ostatok'] != $oper['mavro']) {?><span class='ostatok'><?=$oper['ostatok']?> <?=$oper['mavro_valuta']?></span><?php } ?><?=$oper['mavro']?> <?=$oper['mavro_valuta']?> <?php if($oper['depo']) echo "(".$oper['depo']."m ".$super.")";?><?php } ?></td><?php if($show_frozen) { ?><td class='<?=($is_bonus ? 'bonus' : '').' '.$frozen_gray?>'><?=$show_frozen_row ? getPrintDate($oper['frozen_date']) : ''?></td><td class='<?=($is_bonus ? 'bonus' : '').' '.$frozen_gray?>'><?=$show_frozen_row ? (number_format($oper['frozen_summ'], 2, $USER_SETTING['znak_drob'], ' ')).' '.$VALUTA[$oper['mavro_valuta']] : ''?></td><?php } ?><td class='<?=$is_bonus ? 'bonus' : ''?> o_hranen'><?=$oper['hranen']?>&nbsp;</td><td class='o_dop'><?=$oper['dop']?>&nbsp;</td><?php if($platezh) { ?><td class='o_platezh' id='pl_cell_<?=$oper['id']?>'><?
if(@$oper['_platezh']['id'])
{
	echo number_format($oper['_platezh']['summ'], 2, $USER_SETTING['znak_drob'], ' ').' '.$oper['_platezh']['schet_name'];
}
for($i = 1; $i <= 5; $i++)
{
	if($oper['_platezh']['file'.$i])
	{
		$filename = $oper['_platezh']['file'.$i."_comment"] ? $oper['_platezh']['file'.$i."_comment"] : "Файл ".$i;
		?><br><a href='<?=UPLOAD_PATH.$oper['_platezh']['file'.$i]?>' target=_blank><?=$filename?></a><? 
	}
}
if(@$oper['_add_platezh'])
{
	?><a href='oper/platezh_relation/<?=$oper['id']?>' onclick="openPlatezhRelation(<?=$oper['id']?>); return false;"><img src="<?=config_item('image_path')?>images/icons/chain.png"> Связать с платежом</a><?
}
?></td><?php } ?><?php if($actions || $oper_info) { ?><td class='action'></td><?php } ?></tr>
<?php 
	if($show_time_line)
	{
		$percent_time_line = $oper['frozen_date'] > date('Y-m-d') ? round((time() - strtotime($oper['date'])) / (strtotime($oper['frozen_date']) - strtotime($oper['date'])) * 100) : 100;
		?>
		<tr class='nolist'><td colspan=10  class='time_line_cell'><div class='time_line'><div class='time_line_inner' style="width:<?=$percent_time_line?>%"></div><div class='time_line_left'></div><div class='time_line_right'></div></div></td></tr>
		<?
	}
}
?></tbody><tfoot><?
if($balance)
{
	?><tr><td class='balance' colspan=11>Общий баланс: <?php 
		if(@$user['all_mavro'])
		{
			foreach($user['all_mavro'] as $valuta => $arr_valuta) 
			{ 
				foreach($arr_valuta as $percent => $m) 
				{ 
					echo $m." ".$valuta.' ('.$percent.'%); ';
				}
			}
		}
		?></td></tr>
<?php	}  ?>
</tfoot>
</table>
<?	} 
}
if($actions || $oper_info)
{
	?>
	<script language="javascript">
	var Opers = <?=json_encode($all_opers)?>;
	</script>
	<?
}
?>
<div class='users_delimiter'></div>
